'''
@Author: your name
@Date: 2020-05-16 09:29:02
@LastEditTime: 2020-05-16 11:43:32
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \Gitee\作业\5-15_作业\gelonghui_speak.py
'''
# -*- coding: utf-8 -*-
from time import *
from selenium import webdriver
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# print('selenium version = ', selenium.__version__)
import unittest
from send_mail import *
import os
from HTMLTestRunner import HTMLTestRunner
import random

#-----------格隆汇---------
class Gelonghui_Test(unittest.TestCase):

    def setUp(self):
        desired_caps={
            'platformName':'Android',#手机平台的名称
            'version':'5.1.1',#手机平台的版本
            'deviceName':'127.0.0.1:62001',#这是测试机的型号，可以查看手机的关于本机选项获得adb devices 3ed3c40
            'appPackage':'com.gelonghui.glhapp',#需要测试的app包名
            'appActivity':'components.mainpage.MainActivity', #app启动页面
            'noReset':'True', #不重置  下一次app登录数据，信息等等都在里面。
            # 'app':'D:\\soft\\test\\app\\TensonXCL.apk',
            'automationName':'UiAutomator1',
            'noSign':'True'
            # 'skipServerInstallation':'True',
            # 'skipDeviceInitialization':'True'
        }
        driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)#4723

        def tearDown(self):
            self.driver.quit()
        
        def TestSpeak(self):
            # 等主页面activity出现
            driver.wait_activity("components.mainpage.MainActivity", 20)

            #首页点击社区
            community = driver.find_element_by_id('com.gelonghui.glhapp:id/iv_tab_icon')
            if community:
                print('找到社区，点击')
                community.click()
            else:
                print('没有找到社区')


            #
            # 点击我有话说
            btn_speak = driver.find_element_by_id('com.gelonghui.glhapp:id/tv_want_speak')
            btn_speak.click()
            sleep(2)
            # 点击我也要讲两句
            btn_publish = driver.find_element_by_id('com.gelonghui.glhapp:id/tv_publish')
            btn_publish.click()
            sleep(2)
            
            # 发表内容
            random_number = random.randint(0,1000)
            random_string = random.choice(['天生我材必有用','千金散尽还复来','君不见黄河之水天上来','哈撒给'])
            # 标题
            input_title = driver.find_element_by_id('com.gelonghui.glhapp:id/title')
            # 正文
            input_txt = driver.find_element_by_id('com.gelonghui.glhapp:id/content')
            # 上传照片
            upload_photo = driver.find_element_by_id('com.gelonghui.glhapp:id/img')
            # 本地
            btn_Local = driver.find_element_by_id('com.gelonghui.glhapp:id/btn_choose_local_pic')
            # 选择图片
            select_photo = driver.find_element_by_id('com.gelonghui.glhapp:id/check_view')
            # 确认图片
            btn_confirm = driver.find_element_by_id('com.gelonghui.glhapp:id/button_apply')
            # 发表
            btn_publish = driver.find_element_by_class_name('android.widget.TextView')
            #输入标题
            input_title.send_keys('大咖'+ random_number)
            sleep(1)
            #输入正文
            input_txt.send_keys(random_string)
            sleep(1)
            #上传图片
            upload_photo.click()
            sleep(1)
            # 选择本地
            btn_Local.click()
            sleep(2)
            # 选择图片
            select_photo.click()
            sleep(2)
            # 确认图片
            btn_confirm.click()
            sleep(2)
            # 发表发言
            btn_publish.click()
            sleep(2)

            # 验证结果
            btn_verify = driver.find_element_by_id('com.gelonghui.glhapp:id/tv_publish')
            if btn_verify:
                print('发表成功,继续发表')
            else:
                print('发表失败')
            self.assertEqual(btn_verify,'我也要讲两句')

            
        discover = unittest.defaultTestLoader.discover('./', pattern='*login_test.py', top_level_dir=None)
        if __name__ == "__main__":
                suit = unittest.TestSuite()
                suit.addTest(TestSpeak)
                now = time.strftime('%Y-%m-%d %H-%M-%S')
                file_path = os.path.join("./" + now + 'run_case.html')
                print(file_path)
                
                f = open(file_path, 'wb')
                runner = HTMLTestRunner(stream=f, verbosity=2, title='xxxx测试报告', description='xxxxx测试报告')
                runner.run(discover)
                send_mail(file_path)#发送邮件
                # unittest.main()